package com.hit.server.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.server.api.dto.RobotWarningReadDTO;
import com.hit.server.api.entity.RobotWarningEntity;
import com.hit.server.api.dto.RobotWarningQueryDTO;
import com.hit.server.api.dto.RobotWarningUpdateDTO;
import com.hit.server.api.type.WarningLevelEnums;
import com.hit.server.api.type.WarningStatusEnums;
import com.hit.server.api.type.WarningTypeEnums;
import com.hit.server.api.vo.RobotWarningVO;
import com.hit.server.biz.mapper.RobotWarningMapper;
import com.hit.server.api.service.RobotWarningService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 机器人告警
 *
 * @author kehanjiang
 * @date 2025-04-14 16:24:49
 */
@Service
@RequiredArgsConstructor
public class RobotWarningServiceImpl extends ServiceImpl<RobotWarningMapper, RobotWarningEntity> implements RobotWarningService {

    private RobotWarningVO convertToVO(Object o) {
        if (o instanceof RobotWarningEntity) {
            RobotWarningVO vo = new RobotWarningVO();
            BeanUtil.copyProperties(o, vo);
            //设置其他参数
            Optional.ofNullable(vo.getType()).ifPresent(type -> {
                vo.setTypeCn(WarningTypeEnums.getByCode(type).getDesc());
            });
            Optional.ofNullable(vo.getStatus()).ifPresent(status -> {
                vo.setStatusCn(WarningStatusEnums.getByCode(status).getDesc());
            });
            Optional.ofNullable(vo.getLevel()).ifPresent(level -> {
                vo.setLevelCn(WarningLevelEnums.getByCode(level).getDesc());
            });
            return vo;
        }
        return null;
    }

    private void preCheck(RobotWarningEntity entity, Boolean update) {
        Long id = entity.getId();
        String title = entity.getTitle();
        if (update) {//更新操作
            Assert.notNull(id, "id不可为空！");
        } else {
            Assert.hasText(title, "title不可为空！");
        }
    }

    @Override
    public Page<RobotWarningVO> getRobotWarningPage(Page page, RobotWarningQueryDTO dto) {
        RobotWarningEntity entity = new RobotWarningEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setTitle(null);
        final String title = dto.getTitle();
        LambdaQueryWrapper<RobotWarningEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(title), RobotWarningEntity::getTitle, title);
        if (ArrayUtil.isNotEmpty(dto.getTimes())) {
            wrapper.ge(RobotWarningEntity::getTime, dto.getTimes()[0])
                    .le(RobotWarningEntity::getTime, dto.getTimes()[1]);
        }
        wrapper.orderByDesc(RobotWarningEntity::getTime);
        return (Page<RobotWarningVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }

    @Override
    public RobotWarningVO getRobotWarningById(Long id) {
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }


    @Override
    public Boolean updateRobotWarningById(RobotWarningUpdateDTO dto) {
        RobotWarningEntity entity = new RobotWarningEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);
        return this.updateById(entity);
    }

    @Override
    public Boolean removeRobotWarningBatchByIds(List<Long> ids) {
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }

    @Override
    public Boolean readWarning(RobotWarningReadDTO dto) {
        List<Long> ids = dto.getIds();
        Boolean all = dto.getAll();
        String type = dto.getType();
        Assert.notNull(all, "all不可为空！");
        Assert.hasText(type, "type不可为空！");
        if (!all) {
            Assert.notEmpty(ids, "ids不可为空！");
        }
        LambdaQueryWrapper<RobotWarningEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RobotWarningEntity::getType, type);
        if (!all) {
            wrapper.in(RobotWarningEntity::getId, ids);
        }
        wrapper.eq(RobotWarningEntity::getStatus, WarningStatusEnums.WAIT.getCode());
        RobotWarningEntity entity = new RobotWarningEntity();
        entity.setStatus(WarningStatusEnums.DONE.getCode());
        entity.setUpdateTime(LocalDateTime.now());
        return this.update(entity, wrapper);
    }
}